package com.xinke.module.system.controller.admin.oauth2;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.oauth2.*;
import com.xinke.module.system.model.entity.oauth2.OAuth2AccessTokenDO;
import com.xinke.module.system.service.oauth2.OAuth2AccessTokenService;

@Tag(name = "管理后台 - OAuth2 访问令牌")
@RestController
@RequestMapping("/system/O-auth2-access-token")
@Validated
public class OAuth2AccessTokenController {

    @Resource
    private OAuth2AccessTokenService oAuth2AccessTokenService;

    @PostMapping("/create")
    @Operation(summary = "创建OAuth2 访问令牌")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-access-token:create')")
    public Result<Long> createOAuth2AccessToken(@Valid @RequestBody OAuth2AccessTokenSaveReqVO createReqVO) {
        return success(oAuth2AccessTokenService.createOAuth2AccessToken(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新OAuth2 访问令牌")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-access-token:update')")
    public Result<Boolean> updateOAuth2AccessToken(@Valid @RequestBody OAuth2AccessTokenSaveReqVO updateReqVO) {
        oAuth2AccessTokenService.updateOAuth2AccessToken(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除OAuth2 访问令牌")
    @Parameter(name = "tokenId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:O-auth2-access-token:delete')")
    public Result<Boolean> deleteOAuth2AccessToken(@RequestParam("tokenId") Long tokenId) {
        oAuth2AccessTokenService.deleteOAuth2AccessToken(tokenId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OAuth2 访问令牌")
    @Parameter(name = "tokenId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-access-token:query')")
    public Result<OAuth2AccessTokenRespVO> getOAuth2AccessToken(@RequestParam("tokenId") Long tokenId) {
        OAuth2AccessTokenDO oAuth2AccessToken = oAuth2AccessTokenService.getOAuth2AccessToken(tokenId);
        return success(BeanUtils.toBean(oAuth2AccessToken, OAuth2AccessTokenRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OAuth2 访问令牌分页")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-access-token:query')")
    public Result<PageResult<OAuth2AccessTokenRespVO>> getOAuth2AccessTokenPage(@Valid OAuth2AccessTokenPageReqVO pageReqVO) {
        PageResult<OAuth2AccessTokenDO> pageResult = oAuth2AccessTokenService.getOAuth2AccessTokenPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OAuth2AccessTokenRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出OAuth2 访问令牌 Excel")
    @PreAuthorize("@ss.hasPermission('system:O-auth2-access-token:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOAuth2AccessTokenExcel(@Valid OAuth2AccessTokenPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OAuth2AccessTokenDO> list = oAuth2AccessTokenService.getOAuth2AccessTokenPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "OAuth2 访问令牌.xls", "数据", OAuth2AccessTokenRespVO.class,
                        BeanUtils.toBean(list, OAuth2AccessTokenRespVO.class));
    }

}